package com.adcc.ags.gms.gw.controller;

import com.adcc.ags.gms.core.util.Constant;
import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.gw.service.SysConfigurationService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * Created by zhangPeng on 2017/1/13.
 */

@Controller
@RequestMapping(value = "/sysMonitor", produces = MediaType.APPLICATION_JSON_VALUE)
public class SysMonitorController {

    @Autowired
    private SysConfigurationService sysConfigurationService;

    @Autowired
    private LogFactory log;

    @RequestMapping(value = "/getGWSystemService", method = RequestMethod.GET)
    @ResponseBody private Map<String, Object> getGWSystemService() {
        HttpURLConnection conn = null;
        String gwSysMonitorUrl = "";
        try {
            Map<String, Object> sysMap = Maps.newHashMapWithExpectedSize(2);
            gwSysMonitorUrl = sysConfigurationService.getValue(Constant.SYS_MONITOR_URL);
            conn = (HttpURLConnection) new URL(gwSysMonitorUrl).openConnection();
            conn.setRequestProperty("Accept", "application/json");
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            //调用web服务
            if (conn.getResponseCode() == 200) {
                InputStream inStream = conn.getInputStream();
                byte[] data = read(inStream);
                String objectString = new String(data);
                Map<String, Object> map = new ObjectMapper().readValue(objectString, Map.class);
                if (map == null) {
                    sysMap.put("healthy", false);
                } else {
                    sysMap.put("healthy", true);
                    sysMap.put("message", map);
                }
                return sysMap;
            } else {
                return null;
            }
        } catch (Exception ex) {
            log.error(SysMonitorController.class, "getGWSystemService() error", ex);
            return null;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
    }

    @RequestMapping(value = "/getMQSystemService", method = RequestMethod.GET)
    @ResponseBody private Map<String, Object> getMQSystemService() {
        HttpURLConnection conn = null;
        String MQSysMonitorUrl = "";
        try {
            Map<String, Object> sysMap = Maps.newHashMapWithExpectedSize(2);
            MQSysMonitorUrl = sysConfigurationService.getValue(Constant.MQSYS_MONITOR_URL);
            conn = (HttpURLConnection) new URL(MQSysMonitorUrl).openConnection();
            conn.setRequestProperty("Accept", "application/json");
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            //调用web服务
            if (conn.getResponseCode() == 200) {
                InputStream inStream = conn.getInputStream();
                byte[] data = read(inStream);
                String objectString = new String(data);
                Map<String, Object> map = new ObjectMapper().readValue(objectString, Map.class);
                if (map == null) {
                    sysMap.put("healthy", false);
                } else {
                    sysMap.put("healthy", true);
                    sysMap.put("message", map);
                }
                return sysMap;
            } else {
                return null;
            }
        } catch (Exception ex) {
            log.error(SysMonitorController.class, "getMQSystemService() error", ex);
            return null;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
    }

    /**
     * 读取输入流
     * @param inStream
     * @return
     * @throws Exception
     */
    private byte[] read(InputStream inStream) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outputStream.write(buffer);
        }
        inStream.close();
        return outputStream.toByteArray();
    }

}
